/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.client.websockets.application.boardgame;

import com.playground.client.websockets.entity.message.response.MakeMoveResponseMessage;
import com.playground.client.websockets.entity.message.response.TicTacToeYourTurnResponseMessage;
import com.playground.client.websockets.socket.PlayerSocket;
import com.playground.client.websockets.util.JsonUtil;
import com.playground.core.entity.Game;
import com.playground.core.entity.LoggedPlayer;
import com.playground.core.entity.Player;
import com.playground.core.entity.boardgame.BoardGame;
import com.playground.core.entity.boardgame.Move;
import com.playground.core.entity.boardgame.Square;
import com.playground.core.repository.RoomRepository;
import com.playground.core.service.LoginService;
import com.playground.core.service.factory.GameFactory;
import com.playground.core.service.ruler.CheckersRuler;
import com.playground.core.service.ruler.TicTacToeRuler;
import com.sun.grizzly.tcp.Request;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Cleber
 */
public class TicTacToeApplication extends BoardGameApplication{
    private GameFactory gameFactory;
    private TicTacToeRuler ticTacToeRuler;
    
    public TicTacToeApplication(LoginService loginService, RoomRepository roomRepository,GameFactory gameFactory, TicTacToeRuler ticTacToeRuler){
        super(loginService, roomRepository);
        this.gameFactory = gameFactory;
        this.ticTacToeRuler = ticTacToeRuler;
    }

    @Override
    protected void executeMove(PlayerSocket socket, Move move) {
        BoardGame game = (BoardGame) socket.getPlayer().getRoom().getGame();
        Player madeTheMove = game.getPlayerInTurn();
        MakeMoveResponseMessage msg = new MakeMoveResponseMessage();
        Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "before test...");
        if (ticTacToeRuler.executeMove(game,move)){
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "move made, inside test...");
            msg.setPlayerId(madeTheMove.getId());
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "playerid was set");
            msg.setGameOver(ticTacToeRuler.isGameOver(game));
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "gameover was set");
            msg.setDestinationSquarePosition(move.getDestination().getPosition());
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "destsquare was set");
            msg.setPlayerInTurnId(game.getPlayerInTurn().getId());
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "playerinturn was set");
            msg.setSuccess(true);
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "success was set");
            msg.setWinnerId((null == game.getWinner()) ? null : game.getWinner().getId());
            Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "winner was set");
        } else{
            System.out.println("TicTacToe entrando numa fria!");
            msg.setSuccess(false);
        }
        Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "everything was set. sending message...");
        Logger.getLogger(TicTacToeApplication.class.getName()).log(Level.INFO, "message: " + JsonUtil.getGson().toJson(msg, MakeMoveResponseMessage.class));
        sendToAll(socket.getPlayer().getRoom(), JsonUtil.getGson().toJson(msg, MakeMoveResponseMessage.class));
        informTurn(getPlayerSocketByPlayerId(game.getPlayerInTurn().getId()));
      }

    @Override
    protected void informTurn(PlayerSocket socket) {
    System.out.println("Informing player turn");
        try{
        TicTacToeYourTurnResponseMessage response = new TicTacToeYourTurnResponseMessage();
        response.setSuccess(true);
        BoardGame game = (BoardGame) socket.getPlayer().getRoom().getGame();
        List<Square> movable = ticTacToeRuler.getAllowedSquares(game);
        List<Integer> selectableSquares = new ArrayList<Integer>();
        System.out.println(movable.size());
        for (Square s : movable){
            selectableSquares.add(s.getPosition());
        }
        response.setSelectableSquares(selectableSquares);
        System.out.println(response.getSelectableSquares().size());
        Logger.getLogger(TicTacToeYourTurnResponseMessage.class.getName()).log(Level.INFO, "about to write json");
        String json = JsonUtil.getGson().toJson(response, TicTacToeYourTurnResponseMessage.class);
        Logger.getLogger(TicTacToeYourTurnResponseMessage.class.getName()).log(Level.INFO, "arrived here");
        Logger.getLogger(TicTacToeYourTurnResponseMessage.class.getName()).log(Level.INFO, json);
            System.out.println(json);
        socket.send(json);
        } catch(Exception e){
            e.printStackTrace();
        }}

    @Override
    protected void giveUp(PlayerSocket socket) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public boolean isApplicationRequest(Request rqst) {
        return rqst.requestURI().toString().endsWith("/tictactoe");}

    @Override
    protected Game makeGame(List<LoggedPlayer> players) {
        return gameFactory.createTicTacToe(players);
    }
    
}
